package com.lry.crawlers.configuration;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //交换机名
    public static final String GATHEREXCHANGE_NAME = "gather_exchange";
    public static final String SURVIVEEXCHANGE_NAME = "survive_exchange";
    public static final String UPDATEEXCHANGE_NAME = "update_exchange";

    //队列名
    public static final String FICTIONQUEUE_NAME = "fiction_queue";
    public static final String  VIDEOQUEUE_NAME = "video_queue";
    public static final String  COMICEUE_NAME = "comic_queue";
    public static final String SURVIVEQUEUE_NAME = "survive_queue";
    public static final String FUPDATEEQUEUE_NAME = "Fupdate_queue";
    public static final String CUPDATEEQUEUE_NAME = "Cupdate_queue";
    public static final String VUPDATEEQUEUE_NAME = "Vupdate_queue";


    //fiction交换机
    @Bean("gatherExchange")
    public Exchange fictionExchange(){
        return ExchangeBuilder.topicExchange(GATHEREXCHANGE_NAME).durable(true).build();
    }

    //survive交换机
    @Bean("surviveExchange")
    public Exchange surviveExchange(){
        return ExchangeBuilder.topicExchange(SURVIVEEXCHANGE_NAME).durable(true).build(); }

    //update交换机
    @Bean("updateExchange")
    public Exchange updateExchange(){
        return ExchangeBuilder.topicExchange(UPDATEEXCHANGE_NAME).durable(true).build(); }

    //fiction队列
    @Bean("fictionQueue")
    public Queue fictionQueue(){
        return QueueBuilder.durable(FICTIONQUEUE_NAME).build();
    }

    //video队列
    @Bean("videoQueue")
    public Queue videoQueue(){
        return QueueBuilder.durable(VIDEOQUEUE_NAME).build();
    }
    //comic队列
    @Bean("comicQueue")
    public Queue comicQueue(){
        return QueueBuilder.durable(COMICEUE_NAME).build();
    }
    //survive队列
    @Bean("surviveQueue")
    public Queue surviveQueue(){
        return QueueBuilder.durable(SURVIVEQUEUE_NAME).ttl(10000).build();
    }
    //Fupdate队列
    @Bean("FupdateQueue")
    public Queue FupdateFueue(){
        return QueueBuilder.durable(FUPDATEEQUEUE_NAME).build();
    }
    //Cupdate队列
    @Bean("CupdateQueue")
    public Queue CupdateQueue(){
        return QueueBuilder.durable(CUPDATEEQUEUE_NAME).build();
    }
    //Vupdate队列
    @Bean("VupdateQueue")
    public Queue VupdateQueue(){
        return QueueBuilder.durable(VUPDATEEQUEUE_NAME).build();
    }

    //队列和交互机绑定关系
    @Bean
    public Binding fictionQueueExchange(@Qualifier("fictionQueue") Queue queue, @Qualifier("gatherExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("fiction.#").noargs();
    }

    @Bean
    public Binding videoQueueExchange(@Qualifier("videoQueue") Queue queue, @Qualifier("gatherExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("video.#").noargs();
    }
    @Bean
    public Binding comicQueueExchange(@Qualifier("comicQueue") Queue queue, @Qualifier("gatherExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("comic.#").noargs();
    }
    @Bean
    public Binding surviveQueueExchange(@Qualifier("surviveQueue") Queue queue, @Qualifier("surviveExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("#").noargs();
    }
    @Bean
    public Binding updateFQueueExchange(@Qualifier("FupdateQueue") Queue queue, @Qualifier("updateExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("fiction.#").noargs();
    }
    @Bean
    public Binding updateCQueueExchange(@Qualifier("CupdateQueue") Queue queue, @Qualifier("updateExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("comic.#").noargs();
    }
    @Bean
    public Binding updateVQueueExchange(@Qualifier("VupdateQueue") Queue queue, @Qualifier("updateExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("video.#").noargs();
    }

}